// pages/lose/lose.js
const db = wx.cloud.database(); //初始化数据库
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabNow: 0,
    tabList: ['招领', '寻物'],
    thins: [],
    imgs: [],
    a: '',
    //unfold: null //0为展开，1为收缩
    isPopping: false, //是否已经弹出
    animPlus: {}, //旋转动画
    animCollect: {}, //item位移,透明度
    animTranspond: {}, //item位移,透明度
    animInput: {}, //item位移,透明度
    up_show:false,   //初始值为false 影藏按钮
  },

  // 点击伸缩效果
  /*   unfold() {
      var unfold = this.data.unfold
      if (unfold === null) {  
        unfold = 1   
      }
      if (unfold == 0) {    
        unfold = 1
      } else {
        unfold = 0
      }
      this.setData({  
        unfold: unfold
      })
    }, */
   

 // 监听页面滚动高度，控制回到底部按钮显示/隐藏
  onPageScroll(e){
    var that = this
    if(e.scrollTop>70){
      that.setData({
        up_show:true
      })
    }else{
      that.setData({
        up_show:false
      })
    }
  },
  // 点击回到顶部按钮
  gotopAction(){
    wx.pageScrollTo({
      scrollTop:0
    })
  },
 


 

  selectTab(e) {
    const {
      id
    } = e.currentTarget.dataset;
    this.setData({
      tabNow: id,
    })
    if (id === 0) {
      this.onLoad();
    } else if (id === 1) {
      this.getxunwu();
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this
    // 过审核

    db.collection("a").get()
      .then(res => {
        console.log("11111111", res.data[0].a)
        that.setData({
          a: res.data[0].a,
        })
      })

    wx.showLoading({
      title: '加载中...',
    })
    //const that = this
    db.collection("lose").orderBy("createTime", "desc").where({
        type: "招领"
      }).get()
      .then(res => {
        console.log("全部数据为：", res)
        console.log("全部数据为：", res.data.length)
        that.setData({
          thins: res.data,
        })
      })
    setTimeout(() => {
      wx.hideLoading()
    }, 800)



  },

  // 点击预览图片
  look(e) {
    console.log('图片', e)
    const that = this;
    wx.previewImage({
      current: e.currentTarget.dataset.src,
      urls: [e.currentTarget.dataset.src],

    })
  },
  // 寻物 
  getxunwu() {
    wx.showLoading({
      title: '加载中...',
    })
    const that = this
    db.collection("lose").orderBy("createTime", "desc").where({
        type: "寻物"
      }).get()
      .then(res => {
        console.log("寻物数据为：", res)
        that.setData({
          thins: res.data,
        })
      })
    setTimeout(() => {
      wx.hideLoading()
    }, 800)

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        selected: 3
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    // 没法，只有只有弄一下 
    wx.showLoading({
      title: '加载中...',
    })

    setTimeout(() => {
      wx.hideLoading()
    }, 1500)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log('111', this.data.thins.length)
    if (this.data.tabNow === 0) {
      wx.showLoading({
        title: '加载中...',
      })
      db.collection('lose').orderBy("createTime", "desc").skip(this.data.thins.length).where({
        type: "招领"
      }).get({
        success: (res) => {
          if (res.data.length) {
            res.data.forEach(item => {
              this.data.thins.push(item);
            })
            this.setData({
              thins: this.data.thins,
            })
            wx.hideLoading();
          } else {
            wx.showToast({
              icon: 'none',
              title: '无更多信息',
            })
          }
          //wx.hideLoading();
        },
        fail: (error) => {
          wx.showToast({
            icon: 'none',
            title: '服务器出错...',
          })
          wx.hideLoading();
        }
      })
    } else if (this.data.tabNow === 1) {
      wx.showLoading({
        title: '加载中...',
      })
      db.collection('lose').orderBy("lookNumber", "desc").skip(this.data.thins.length).where({
        type: "寻物"
      }).get({
        success: (res) => {
          if (res.data.length) {
            res.data.forEach(item => {
              this.data.thins.push(item);
            })
            this.setData({
              thins: this.data.thins,
            })
            wx.hideLoading();
          } else {
            wx.showToast({
              icon: 'none',
              title: '无更多信息',
            })
          }
          //wx.hideLoading();
        },
        fail: (error) => {
          wx.showToast({
            icon: 'none',
            title: '服务器出错...',
          })
          wx.hideLoading();
        }
      })
    }

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '失物招领',
      path: 'pages/lose/lose'
    }
  },
  // 发布 
  send() {
    wx.navigateTo({
      url: '/pages/sendlose/sendlose',
    })
  },

  // 动画效果
  //点击弹出
  plus: function () {
    if (this.data.isPopping) {
      //缩回动画
      this.popp();
      this.setData({
        isPopping: false
      })
    } else if (!this.data.isPopping) {
      //弹出动画
      this.takeback();
      this.setData({
        isPopping: true
      })
    }
  },

  input: function () {
    console.log("input")
  },
  transpond: function () {
    console.log("transpond")
  },
  collect: function () {
    console.log("collect")
  },

  //弹出动画
  popp: function () {
    //plus顺时针旋转
    var animationPlus = wx.createAnimation({
      duration: 500,
      timingFunction: 'ease-out'
    })
    var animationcollect = wx.createAnimation({
      duration: 500,
      timingFunction: 'ease-out'
    })
    var animationTranspond = wx.createAnimation({
      duration: 500,
      timingFunction: 'ease-out'
    })
    var animationInput = wx.createAnimation({
      duration: 500,
      timingFunction: 'ease-out'
    })
/*     animationPlus.rotateZ(220).step();
    animationcollect.translate(-100, -100).rotateZ(180).opacity(1).step();
    animationTranspond.translate(-140, 0).rotateZ(180).opacity(1).step();
    animationInput.translate(-100, 100).rotateZ(180).opacity(1).step(); */

    animationPlus.rotateZ(220).step();
    animationcollect.translate(-100, -100).rotateZ(180).opacity(1).step();
    animationTranspond.translate(0, -100).rotateZ(180).opacity(1).step();
    animationInput.translate(100, -100).rotateZ(180).opacity(1).step();


    this.setData({
      animPlus: animationPlus.export(),
      animCollect: animationcollect.export(),
      animTranspond: animationTranspond.export(),
      animInput: animationInput.export(),
    })
  },
  //收回动画
  takeback: function () {
    //plus逆时针旋转
    var animationPlus = wx.createAnimation({
      duration: 500,
      timingFunction: 'ease-out'
    })
    var animationcollect = wx.createAnimation({
      duration: 500,
      timingFunction: 'ease-out'
    })
    var animationTranspond = wx.createAnimation({
      duration: 500,
      timingFunction: 'ease-out'
    })
    var animationInput = wx.createAnimation({
      duration: 500,
      timingFunction: 'ease-out'
    })
    animationPlus.rotateZ(0).step();
    animationcollect.translate(0, 0).rotateZ(0).opacity(0).step();
    animationTranspond.translate(0, 0).rotateZ(0).opacity(0).step();
    animationInput.translate(0, 0).rotateZ(0).opacity(0).step();
    this.setData({
      animPlus: animationPlus.export(),
      animCollect: animationcollect.export(),
      animTranspond: animationTranspond.export(),
      animInput: animationInput.export(),
    })
  },



})